<template>
  <div>
    <p>请选择你要购买的书籍</p>
    <ul>
        <li v-for="val in arr" :key="val.id" >
            <p>{{val.name}} <button @click="btn(val.id)">{{msg}}</button></p> 
        </li>
       
    </ul>
    <table border="1" width="500" cellspacing="0">
      <tr >
        <th>序号</th>
        <th>书名</th>
        <th>单价</th>
        <th>数量</th>
        <th>合计</th>
      </tr>
      <tr v-for="val in arr" :key="val.id">
          <td>{{val.id}}</td>
          <td>{{val.name}}</td>
          <td>{{val.price}}</td>
          <td>{{val.num}}</td>
          <td>{{val.price * val.num}}</td>
      </tr>
    </table>
    <p>总价格为:{{ zongjia }} </p>
  </div>
</template>

<script>
export default {
  data() {
    return {
        msg:'买书',
      arr: [
        {
          id:1,
          name: "水浒传",
          price: 107,
          num:0,
          count: 0,
          
         
        },
        {
          id:2,
          name: "西游记",
          price: 192,
          num:0,
          count: 0,
          
        },
        {
          id:3,
          name: "三国演义",
          price: 219,
          num:0,
          count: 0,
          
        },
        {
          id:4,  
          name: "红楼梦",
          price: 178,
          num:0,
          count: 0,
           
        },
      ],
    };
  },
  methods: {
      btn(index){
          this.arr[index-1].num =  this.arr[index-1].num + 1
          
      }
  },
  computed:{
      zongjia(){
          return this.arr.reduce((sum,obj) => {
              return sum += obj.price * obj.num
          },0)
      }
  }
};
</script>